/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.action.system;

import net.sf.json.JSONObject;
import org.grayrabbit.web.struts2.GenericAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.grayrabbit.pay.core.model.results.GenericsResult;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.grayrabbit.pay.core.model.pages.PageList;
import org.grayrabbit.pay.core.model.entity.system.SystemConfig;
import org.grayrabbit.pay.core.biz.manager.system.SystemConfigManager;

/**
 * 
 * 
 * @author liyd
 * @version $Id: SystemConfigAction.java, v 0.1 Fri Jun 01 11:14:01 CST 2012 liyd Exp $
 */
@Results({
          @Result(name = GenericAction.RELOAD, location = "system_config_list", type = "redirectAction"),
          @Result(name = GenericAction.INPUT, location = "system_config-input.vm"),
          @Result(name = GenericAction.LIST, location = "system_config-list.vm") })
@InterceptorRefs({ @InterceptorRef(value = "token", params = { "includeMethods", "save" }),
                  @InterceptorRef("login"), @InterceptorRef("defaultStack") })
public class SystemConfigAction extends GenericAction {

    /** serialVersionUID */
    private static final long      serialVersionUID = 9141051077210127605L;

    /** 业务对象manager接口 */
    @Autowired
    private SystemConfigManager    systemConfigManager;

    /** 业务实体对象 */
    private SystemConfig           systemConfig     = new SystemConfig();

    /** 业务实体对象列表 */
    private PageList<SystemConfig> systemConfigList;

    /**
     * 查询列表
     * 访问URL /system_config_list.action
     * 
     * @return
     */
    @Action(value = "system_config_list")
    public String list() {

        GenericsResult<PageList<SystemConfig>> result = null;
        result = systemConfigManager.queryPageList(systemConfig);

        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
        }
        systemConfigList = result.getResultData();
        return LIST;
    }

    /**
     * 跳转到添加修改页面
     * 访问URL /system_config_input.action
     * 
     * @return
     */
    @Action(value = "system_config_input")
    public String input() {

        if (systemConfig.getConfigId() == null) {
            return INPUT;
        }

        GenericsResult<SystemConfig> result = systemConfigManager.getById(systemConfig
            .getConfigId());

        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
        }
        systemConfig = result.getResultData();

        return INPUT;
    }

    /**
     * 保存
     * 访问URL /system_config_save.action
     * 
     * @return
     */
    @Action(value = "system_config_save")
    public String save() {

        PayResult result = null;

        if (systemConfig.getConfigId() != null) {
            result = systemConfigManager.update(systemConfig);
        } else {
            result = systemConfigManager.insert(systemConfig);
        }

        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
            return INPUT;
        }
        return RELOAD;
    }

    /**
     * 删除
     * 访问URL /system_config_delete.action
     * 
     * @return
     */
    @Action(value = "system_config_delete")
    public String delete() {

        PayResult result = systemConfigManager.deleteBatchById(key);
        JSONObject jsonObject = JSONObject.fromObject(result);

        return renderJson(jsonObject.toString());
    }

    /**
     * Getter method for property <tt>systemConfig</tt>.
     * 
     * @return property value of systemConfig
     */
    public SystemConfig getSystemConfig() {
        return systemConfig;
    }

    /**
     * Setter method for property <tt>systemConfig</tt>.
     * 
     * @param systemConfig value to be assigned to property systemConfig
     */
    public void setSystemConfig(SystemConfig systemConfig) {
        this.systemConfig = systemConfig;
    }

    /**
     * Getter method for property <tt>systemConfigList</tt>.
     * 
     * @return property value of systemConfigList
     */
    public PageList<SystemConfig> getSystemConfigList() {
        return systemConfigList;
    }

    /**
     * Setter method for property <tt>systemConfigList</tt>.
     * 
     * @param districtList value to be assigned to property systemConfigList
     */
    public void setSystemConfigList(PageList<SystemConfig> systemConfigList) {
        this.systemConfigList = systemConfigList;
    }

}
